var mysqlUtil = require('../util/mysqlUtil');
var authUtil = require('../util/authUtil');

/**
 * 获取升级项目的列表
 */
var getUpgradeProjects = function(req, res) {
    var sql = "select id as code, name, code as keyword1, '' as keyword2 from t_ug_project order by code";

    mysqlUtil.exec(sql, [], function(err, result) {
        var obj = {
            datas: result,
            key: req.query.key
        };

        res.writeHead(200, { 'Content-Type': 'application/json' });
        res.end(JSON.stringify(obj));
    });
};

/**
 * 获取升级项目的列表
 */
var getTestStatus = function(req, res) {
    var sql = "select code, name, '' as keyword1, '' as keyword2 from t_ug_teststatus order by px";

    mysqlUtil.exec(sql, [], function(err, result) {
        var obj = {
            datas: result,
            key: req.query.key
        };

        res.writeHead(200, { 'Content-Type': 'application/json' });
        res.end(JSON.stringify(obj));
    });
};

/**
 * 获取svn开发者
 * 
 */
var getDevelopers = function(req, res) {
    var sql = `
        select id as code, name, '' as keyword1, '' as keyword2 from (
            select u.id, concat(name, ' (', (select count(1) from t_ug_simple_package p where p.leader_userid=u.id), ')') as name 
            from t_user u where is_developer=1 order by code asc
        ) datas
    `;

    mysqlUtil.exec(sql, [], function(err, result) {
        var obj = {
            datas: result,
            key: req.query.key
        };

        res.writeHead(200, { 'Content-Type': 'application/json' });
        res.end(JSON.stringify(obj));
    });
};

/**
 * 获取svn开发者
 * 
 */
var getSvnUser = function(req, res) {
    var sql = "select code, name, '' as keyword1, '' as keyword2 from svn_user";

    mysqlUtil.exec(sql, [], function(err, result) {
        var obj = {
            datas: result,
            key: req.query.key
        };

        res.writeHead(200, { 'Content-Type': 'application/json' });
        res.end(JSON.stringify(obj));
    });
};

exports.getUpgradeProjects = getUpgradeProjects;
exports.getTestStatus = getTestStatus;
exports.getDevelopers = getDevelopers;
exports.getSvnUser = getSvnUser;